package httpRequestUtil

import (
	"bytes"
	"io"
	"net/http"
)

/* 封装http请求通用方法 */
func HttpRequest(requestType string, url string, headers map[string]string, body string) (status string, requestData string) {
	client := &http.Client{}
	req, err := http.NewRequest(requestType, url, bytes.NewBuffer([]byte(body)))
	if err != nil {
		panic(err)
	}
	//设置请求头
	if requestType != "GET" {
		req.Header.Set("Content-Type", "application/json")
	}
	if headers != nil {
		for headerKey, headerVal := range headers {
			req.Header.Set(headerKey, headerVal)
		}
	}
	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// 读取响应体
	requestBody, requestErr := io.ReadAll(resp.Body)
	if requestErr != nil {
		panic(requestErr)
	}
	return resp.Status, string(requestBody)
}
